package vcp.views;


import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;

import client.Message;
import db.parking.beans.Complaints;
import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.controllers.ResolveComplaintsController;
import vcp.gui.panels.customerservice.PnlResolveComplaints;

/**
 * Resolve Complaints View
 *
 */
public class ResolveComplaintsView extends AbstractView implements Observer,
		IView {
	boolean complaintsTableLoaded = false;
	PnlResolveComplaints pnlResolveComplaints;
	Object controller;

	public ResolveComplaintsView() {
		pnlResolveComplaints = new PnlResolveComplaints();
	}

	public PnlResolveComplaints getComplaintsForm() {
		return pnlResolveComplaints;
	}

	@Override
	public void addController(Object obj) {
		// need to listen if parent panel loaded
		ResolveComplaintsController controller = (ResolveComplaintsController)obj;
		this.pnlResolveComplaints.addAncestorListener(
				(AncestorListener) controller);
		this.pnlResolveComplaints.getComplaintsTable().getDtm().addTableModelListener((TableModelListener)obj);
		this.pnlResolveComplaints.getBtnCompensation().addActionListener((ActionListener)obj);
		this.pnlResolveComplaints.getBtnCompensation().setActionCommand("BtnCompensate");
		this.controller = obj;
	}

	@Override
	public void update(Observable arg0, Object arg) {
		Message recvmsg = (Message) arg;
		String func = recvmsg.getMsg();
		Object robj=recvmsg.getObj();
		if (func.equals("GetComplaints") && recvmsg.isStatus() && robj!=null && complaintsTableLoaded==false) {
			ArrayList<Complaints> list = 
					(ArrayList<Complaints>) recvmsg.getObj();
			java.util.Iterator<Complaints> itty = list.iterator();
			Object obj[] = new Object[6];
			while (itty.hasNext()) {
				Complaints c = (Complaints) itty.next();
				obj[0] = c.getComplaintID();
				obj[1] = c.getDescription();
				obj[2] = c.getCustomerID();
				obj[3] = c.getOrderID();
				obj[4] = c.getTitle();
				if(c.getStatus()==0){
					obj[5] = new Boolean(false);
				}else{
					obj[5] = new Boolean(true);
				}
				this.pnlResolveComplaints.getComplaintsTable().getDtm().addRow(obj);
				
			}
			
			complaintsTableLoaded=true;
			this.pnlResolveComplaints.getComplaintsTable().getSelectionModel().
            addListSelectionListener((ListSelectionListener)this.controller);
		}
		
	}
    
	
}
